"""
8 京东二面笔试题
1） 生成一个大文件ip.txt,要求1200行，每行随机为172.25.254.1---172.25.254.254之间的一个ip地址;
2） 读取ip.txt文件统计这个文件中ip出现频率排前10的ip
"""
import random

def generate_ip():
    ip = '172.25.254.'
    n = str(random.randint(1,254))
    ip = ip + n
    return ip


# with open("ip.txt", mode='w', encoding='utf-8') as f:
#     for i in range(1200):
#         ip = generate_ip()
#         f.write(ip)
#         f.write('\n')


with open('ip.txt', mode='r', encoding='utf-8') as f:
    ip_list = f.read().strip().split('\n')
    # print(ip_list)
    d = {}
    for ip in ip_list:
        d[ip] = d.get(ip, 0) + 1

    ls = list(d.items())
    ls.sort(key=lambda x:x[1], reverse=True)

    for k in ls[:10]:
        print("{}->{}个".format(k[0],k[1]))
